package com.smart.stock.common.util;

import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * 通用返回对象
 * @Author wlj @Date 2020/1/2 2:55 下午
 */
@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private long timestamp = System.currentTimeMillis();

    private int code;

    private String msg;

    private T data;

    private int total;

    private Boolean flashDict;

    public Result() {
        this.code = ResultCode.SUCCESS.getCode();
        this.msg = ResultCode.SUCCESS.getMsg();
    }

    public Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Result(int code, String msg, List data) {
        this.code = code;
        this.msg = msg;
        this.data = (T) data;
        this.total = data.size();
    }
    /**
     * 成功返回结果
     * @param data 获取的数据
     */
    public static <T> Result<T> success (T data) {
        return new Result<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), data);
    }

    /**
     * 成功返回结果
     * @param data 获取的数据
     * @param msg 提示信息
     */
    public static <T> Result<T> success (T data, String msg) {
        return new Result<>(ResultCode.SUCCESS.getCode(), msg, data);
    }

    /**
     * 失败返回结果
     */
    public static <T> Result<T> failed () {
        return new Result<>(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMsg());
    }

    /**
     * 失败返回结果
     * @param msg 提示信息
     */
    public static <T> Result<T> failed (String msg) {
        return new Result<>(ResultCode.FAILED.getCode(), msg);
    }

    /**
     * 失败返回结果
     * @param code 提示code
     * @param msg 提示信息
     */
    public static <T> Result<T> failed (int code, String msg) {
        return new Result<>(code, msg);
    }

    /**
     * 参数验证失败返回结果
     * @param msg 提示信息
     */
    public static <T> Result<T> validateFailed (String msg, T failed) {
        return new Result<>(ResultCode.VALIDATE_FAILED.getCode(), msg, failed);
    }

    /**
     * 未登录返回结果
     */
    public static <T> Result<T> unauthorized (T data) {
        return new Result<>(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMsg(), data);
    }

    /**
     *  未授权返回结果
     */
    public static <T> Result<T> forbidden (T data) {
        return new Result<>(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMsg(), data);
    }

    /**
     * 通过Boolean查询是否返回失败或正确的
     * @param isOk 结果
     * @param msg 提示信息
     * @return
     */
    public static <T> Result<Boolean> resultByBoolean(Boolean isOk, String msg) {
        if (isOk) {
            return Result.success(isOk, msg + "成功");
        }
        return Result.failed(msg + "失败");
    }

    /**
     * 通过 > 0 否返回失败或正确的
     * @param number 结果
     * @param msg 提示信息
     * @return
     */
    public static Result<Integer> resultByInt(Integer number, String msg) {
        if (number > 0) {
            return Result.success(number, msg + "成功");
        }
        return Result.failed(msg + "失败, 无数据");
    }
}
